package com.zhouqinuan.spring.cloud.develop.framework.base.config.swagger;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration;


@Configuration
@EnableConfigurationProperties(ApiDocsProperties.class)
@ConditionalOnProperty(prefix = "api", name = "docs", havingValue = "enable", matchIfMissing = false)
@Import({Swagger2DocumentationConfiguration.class})
public class ApiDocsConfiguration {
	
	@Autowired
	private ApiDocsProperties apiDocsProperties;
	
	@Bean
    public Docket createRestApi() {
 
	    //添加head参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<Parameter>();
        parameterBuilder.name("Accept-Language").description("选择语言").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        parameters.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage(StringUtils.isBlank(apiDocsProperties.getBasePackage())? "com.zhouqinuan" : apiDocsProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
	}
}
